<?php
namespace Tbf\Bundle\ImageBundle\Tests;
use Symfony\Component\Yaml\Yaml;
use Tbf\Bundle\TestBundle\TestSuite\TbfTestCase;

class ResizePictureManagerTest extends TbfTestCase
{
    function setUp(){
        $this->markTestIncomplete();
    }
    /**
     * @dataProvider getFixData
     */
    public function testCountResizeSize($data)
    {
        $ret = $this->get('tbf_shop.model.image.resize_picture')
            ->countResizeSize($data['input']);
        $this->assertEquals($data['output'],$ret);
    }

    protected $fix_data_yaml = <<<'YAML'
# 尺寸小于不修改图像
-
    input: {input_width:100,input_height:100,need_width:100,need_height:100}
    output: {width:100,height:100,need_resize:false}
-
    input: {input_width:50,input_height:99,need_width:100,need_height:100}
    output: {width:50,height:99,need_resize:false}
# iw>nw && ah<nh
-
    input: {input_width: 300,input_height: 200,need_width: 100,need_height: 100}
    output: {width: 100,height: 66.666666666667,need_resize: true}
# iw>nw && ah>nh
-
    input: {input_width: 100,input_height: 100,need_width: 50,need_height: 20}
    output: {width: 20,height: 20,need_resize: true}
# iw<nw && ih>nh && aw<nw
-
    input: {input_width: 100,input_height: 100,need_width: 200,need_height: 50}
    output: {width: 50,height: 50,need_resize: true}
YAML;
    public function getFixData()
    {
        $output = Yaml::parse($this->fix_data_yaml);
        foreach ($output as $key=>$row) {
            $output[$key] = array($row);
        }

        return $output;
    }

    protected $fix_data_xml = <<<'XML'
<data>
<testCase>
    <input input_width="100" input_height="100" need_width="100" need_height="100" />
    <output width="100" height="100" need_resize="false" />
</testCase>
<testCase>
    <input input_width="50" input_height="99" need_width="100" need_height="100" />
    <output width="50" height="99" need_resize="false" />
</testCase>
</data>
XML;
    public function getFixData1()
    {
        $dom = new \DOMDocument;
        $dom->loadXML($this->fix_data_xml);
        $data = $dom->childNodes;
        $data = $data->item(0)->childNodes;
        $output = array();
        foreach ($data as $testcase) {
            if ($testcase instanceof \DOMText) {
                continue;
            }
            $d1 = $testcase->childNodes;
            $this_output = array();
            foreach ($d1 as $d2) {
                if ($d2 instanceof \DOMText) {
                    continue;
                }
                $d3  = $d2->attributes;
                $row = array();
                foreach ($d3 as $d4) {
                    $key = $d4->name;
                    $value = $d4->value;
                    $row[$key] = $value;
                }
                $this_output[$d2->tagName] = $row;
            }
            $output[] = array($this_output);
        }
    }

    public function getFixData2()
    {
        $dom = simplexml_load_string($this->fix_data_xml);
        $output = array();
        foreach ($dom as $testcase) {
            $this_output = array();
            foreach ($testcase as $k1=>$v1) {
                foreach ($v1->attributes() as $k2=>$v2) {
                    $this_output[$k1][$k2] = (string) $v2;
                }
            }
            $output[] = array($this_output);
        }

        return $output;
    }

}
